package com.faceCaptor.main;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.faceCaptor.util.DateUtil;
import com.faceCaptor.util.HttpUtil;
import com.faceCaptor.util.JsonUtil;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

import java.util.Calendar;
import java.util.TimeZone;

public class RecordsDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private List list;
	private String userId;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	
	public RecordsDialog(Shell parent,String userId) {
		super(parent);
		this.userId=userId;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */

	/**
	 * Create contents of the dialog.
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridData data = new GridData(SWT.DEFAULT, 600);

		// data.
		container.setLayoutData(data);
		GridLayout gl_container = new GridLayout(5, true);
		gl_container.marginLeft = 20;
		gl_container.marginRight = 20;
		gl_container.marginTop = 20;
		container.setLayout(gl_container);

		CLabel label = new CLabel(container, SWT.NONE);
		data = new GridData();
		data.horizontalAlignment = SWT.CENTER;
		label.setText("起始日期:");
		label.setLayoutData(data);

		DateTime dateTime = new DateTime(container, SWT.NONE);
		Calendar calendar =Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
		dateTime.setDate(calendar.get(Calendar.YEAR)-1, calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		// GridData gridData = new GridData();
		// gridData.horizontalAlignment = GridData.FILL;
		// gridData.grabExcessHorizontalSpace = true;
		// dateTime.setLayoutData(gridData);

		CLabel label_1 = new CLabel(container, SWT.NONE);
		label_1.setText("截止日期:");
		label_1.setLayoutData(data);

		DateTime dateTime_1 = new DateTime(container, SWT.NONE);

		Button button = new Button(container, SWT.NONE);
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		button.setLayoutData(data);

		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				list.removeAll();
				int dm = dateTime.getMonth() + 1;
				int dm1 = dateTime_1.getMonth() + 1;
				String start = "" + dateTime.getYear() + "-" + dm + "-" + dateTime.getDay();
				String end = "" + dateTime_1.getYear() + "-" + dm1 + "-" + dateTime_1.getDay();
				String reqUrl = HttpUtil.HOST + HttpUtil.REMARKHISTORY + userId + "?start=" + start
						+ "&end=" + end;
				String jsonList = HttpUtil.sendGetRequest(reqUrl);
//				System.out.println(jsonList);
				if (jsonList != null) {
					JSONArray arr = JsonUtil.toJsonArray(jsonList, "data");
					if (arr != null) {

						for (int i = 0; i < arr.size(); i++) {
							StringBuffer stringBuffer = new StringBuffer();
							JSONObject jsonObject = (JSONObject) arr.get(i);
							String millisTime = jsonObject.getString("createTime");
							String time = DateUtil.millisToDate(millisTime);
							stringBuffer.append(time);
							stringBuffer.append("      ");
							stringBuffer.append(jsonObject.getString("detail"));
							list.add(stringBuffer.toString());

						}
						
					}
					

				}

			}
		});
		button.setText("查询");

		ListViewer listViewer = new ListViewer(container, SWT.BORDER | SWT.H_SCROLL|SWT.V_SCROLL);
		// listViewer.set
		list = listViewer.getList();

		
		GridData gd_list = new GridData(SWT.LEFT, SWT.CENTER, false, false, 5, 5);
		gd_list.horizontalAlignment = SWT.FILL;
		gd_list.verticalAlignment = SWT.FILL;
		gd_list.grabExcessVerticalSpace = true;
		list.setLayoutData(gd_list);

		return container;

	}
	
	@Override
    protected Point getInitialLocation(Point initialSize){
    	Point location = new Point(this.getParentShell().getLocation().x  
                + 150, this.getParentShell().getLocation().y  
                + 140); 
    	return location;  


    }
	
	@Override
    protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.OK_ID, "确认", true);
        
    }
	
}
